<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');

$sql="
    insert into ns_v1myorder
SELECT
    am.`e_oid`,
    am.`trackId`,
    am.`createdDate`,
    am.`flag`,
    am.`type`,
    am.`date`,
    am.`disposaldate`,
    am.`disposaluserAccount`,
    am.`disposalstatus`,
    am.`disposalmessage`,
    am.`price`,
    am.`phototrackId`,
    am.`photoname`,
    am.`photoprice`,
    am.`photohours`,
    am.`photopersons`,
    am.`photomakeup`,
    am.`photooriginal`,
    am.`videotrackId`,
    am.`videoname`,
    am.`videoprice`,
    am.`videohours`,
    am.`videopersons`,
    am.`videomakeup`,
    am.`videooriginal`,
    am.`note`,
    ai.`customerId`,
    am.`finished`,
    am.`discount`,
    am.`isOrderDiscountSolved`,
    am.`isPayment1FirstSolved`,
    am.`isPayment1SecondSolved`,
    am.`isPayment1ThirdSolved`,
    am.`isPayment1ForthSolved`,
    am.`isPayment2FirstSolved`,
    am.`isPayment2SecondSolved`,
    am.`isPayment3FirstSolved`,
    am.`isPayment3SecondSolved`,
    am.`isPayment4Solved`,
    am.`isBonusPaid`,
    am.`admAppr`,
    am.`discAdmAppr`,
    am.`isNoWeddingDaySolved`
FROM
(SELECT
    nc.`e_oid`,
    nc.`trackId`,
    nc.`createdDate`,
    nc.`flag`,
    nc.`type`,
    nc.`date`,
    nc.`disposaldate`,
    nc.`disposaluserAccount`,
    nc.`disposalstatus`,
    nc.`disposalmessage`,
    nc.`price`,
    np.`phototrackId`,
    np.`photoname`,
    np.`photoprice`,
    np.`photohours`,
    np.`photopersons`,
    np.`photomakeup`,
    np.`photooriginal`,
    np.`videotrackId`,
    np.`videoname`,
    np.`videoprice`,
    np.`videohours`,
    np.`videopersons`,
    np.`videomakeup`,
    np.`videooriginal`,
    nc.`note`,
    nc.`customerId`,
    nc.`finished`,
    nc.`discount`,
    nc.`isOrderDiscountSolved`,
    nc.`isPayment1FirstSolved`,
    nc.`isPayment1SecondSolved`,
    nc.`isPayment1ThirdSolved`,
    nc.`isPayment1ForthSolved`,
    nc.`isPayment2FirstSolved`,
    nc.`isPayment2SecondSolved`,
    nc.`isPayment3FirstSolved`,
    nc.`isPayment3SecondSolved`,
    nc.`isPayment4Solved`,
    nc.`isBonusPaid`,
    nc.`admAppr`,
    nc.`discAdmAppr`,
    nc.`isNoWeddingDaySolved`
FROM
    (
        SELECT
            nm.*, dia.*
        FROM
            ns_myorder AS nm,
            (
                SELECT
                    aa.date AS disposaldate,
                    aa.userAccount AS disposaluserAccount,
                    aa.`status` AS disposalstatus,
                    aa.message AS disposalmessage,
                    bb.oid_a AS order_id
                FROM
                    ns_myordercancel AS aa
                LEFT JOIN ns__ez_relation_myorder_myordercancel AS bb ON aa.e_oid = bb.oid_b
            )AS dia
        WHERE
            nm.e_oid = dia.order_id
    )AS nc,
    (
        SELECT
            pm.`phototrackId`,
            pm.`photoname`,
            pm.`photoprice`,
            pm.`photohours`,
            pm.`photopersons`,
            pm.`photomakeup`,
            pm.`photooriginal`,
            vm.`videotrackId`,
            vm.`videoname`,
            vm.`videoprice`,
            vm.`videohours`,
            vm.`videopersons`,
            vm.`videomakeup`,
            vm.`videooriginal`,
            vm.vorderid
        FROM
            (
                SELECT
                    a.trackId AS phototrackId,
                    a.`name` AS photoname,
                    a.price AS photoprice,
                    a.hours AS photohours,
                    a.persons AS photopersons,
                    a.makeup AS photomakeup,
                    a.original AS photooriginal,
                    b.oid_a AS porderid
                FROM
                    ns_myorderpackage AS a
                LEFT JOIN ns__ez_relation_myorder_myorderpackage AS b ON a.e_oid = b.oid_b
                WHERE
                    b.var_a = 'photo'
            )AS pm,
            (
                SELECT
                    a.trackId AS videotrackId,
                    a.`name` AS videoname,
                    a.price AS videoprice,
                    a.hours AS videohours,
                    a.persons AS videopersons,
                    a.makeup AS videomakeup,
                    a.original AS videooriginal,
                    b.oid_a AS vorderid
                FROM
                    ns_myorderpackage AS a
                LEFT JOIN ns__ez_relation_myorder_myorderpackage AS b ON a.e_oid = b.oid_b
                WHERE
                    b.var_a = 'video'
            )AS vm
        WHERE
            pm.porderid = vm.vorderid
    )AS np
WHERE
    nc.e_oid = np.vorderid) AS am LEFT JOIN
( SELECT a.e_oid,IFNULL(b.oid_a,0) AS customerId FROM ns_myorder AS a LEFT JOIN ns__ez_relation_customer_myorder  AS b
ON a.e_oid = b.oid_b ORDER BY b.oid_a DESC ) AS ai
ON am.e_oid = ai.e_oid
";

sql_execute($sql);
